import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: () => import('../components/Login.vue') },
  {
    path: '/home',
    component: () => import('../components/Home.vue'),
    redirect: '/wellcome',
    children: [
      { path: '/wellcome', component: () => import('../components/Wellcome.vue') },
      { path: '/users', component: () => import('../components/users/Users.vue') },
      { path: '/rights', component: () => import('../components/power/Rights.vue') },
      { path: '/roles', component: () => import('../components/power/Roles.vue') },
      { path: '/categories', component: () => import('../components/goods/Category.vue') },
      { path: '/params', component: () => import('../components/goods/Params.vue') },
      { path: '/goods', component: () => import('../components/goods/Goods.vue') },
      { path: '/goods/add', component: () => import('../components/goods/Add.vue') },
      { path: '/orders', component: () => import('../components/order/Order.vue') },
      { path: '/reports', component: () => import('../components/report/Report.vue') }
    ]
  },
  { path: '/test', component: () => import('../components/test.vue') }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to将要访问的路径
  // from从哪个路径跳转过来
  // next是一个函数表示执行
  // next() 放行   next('/') 表示强制跳转的路径
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
